package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @Description:
 * @Author llf
 * @Date 2022/6/17 22:59
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    //TODO
    @Override
    public HomePageResponse homepage() {
        Set<PanelDto> panelDtoSet = new HashSet<>();
        HomePageResponse homePageResponse = new HomePageResponse();
        List<Panel> panelList = panelMapper.selectAll();
        for (Panel panel : panelList) {
            Integer id = panel.getId();
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(id);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            try{
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtoSet.add(panelDto);}
            catch (Exception e){
                throw new NullPointerException();
            }
        }
        homePageResponse.setCode("000000");
        homePageResponse.setMsg("成功");
        homePageResponse.setPanelContentItemDtos(panelDtoSet);
        return homePageResponse;
    }
}
